package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private IYddUserService yddUserService;

    /**
     * 会员列表分页查询
     *
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData getList(String sex, String username, String mobile, String page, String limit) {

        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(username), YddUser::getUsername, username);
        queryWrapper.likeRight(StringUtils.isNotEmpty(mobile), YddUser::getMobile, mobile);
        queryWrapper.eq(StringUtils.isNotEmpty(sex), YddUser::getSex, sex);

        long currentPage = Long.parseLong(page);
        long pageSizes = Long.parseLong(limit);
        Page yddUserPage = new Page<YddUser>(currentPage, pageSizes);

        yddUserService.page(yddUserPage, queryWrapper);

        List<YddUser> records = yddUserPage.getRecords();

        if (records.size() == 0) {
            return ResultData.error("数据为空");
        }
        records = records.stream().map(item->{
            item.setIcon("http://localhost/undefined"+item.getIcon());
            return item;
        }).collect(Collectors.toList());

        yddUserPage.setRecords(records);

        return ResultData.ok("查询成功", PageResult.getPage(yddUserPage));
    }
    /**
     * 查看用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {

        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getId, id);

        YddUser yddUser = yddUserService.getOne(queryWrapper);
        if (yddUser == null) {
            return ResultData.error("找不到用户信息");
        }
        yddUser.setPassword("");
        return ResultData.ok("查询成功", yddUser);
    }

    /**
     * 添加用户
     *
     * @param yddUser
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody YddUser yddUser) {
        String password = yddUser.getPassword();
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        yddUser.setPassword(md5Pass);

        boolean save = yddUserService.save(yddUser);
        if (save) {
            return ResultData.ok("新增成功", "新增成功");
        }
        return ResultData.error("新增失败");
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData delete(Integer id) {
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getId, id);

        YddUser one = yddUserService.getOne(queryWrapper);
        if (one == null) {
            return ResultData.error("删除失败");
        }
        yddUserService.remove(queryWrapper);

        return ResultData.ok("删除成功", "删除成功");
    }

    /**
     * 用户信息修改
     * @param yddUser
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddUser yddUser) {
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        if (yddUser.getId() != null) {
            queryWrapper.eq(YddUser::getId, yddUser.getId());

            YddUser one = yddUserService.getOne(queryWrapper);
            if (one != null) {
                if(yddUser.getPassword() == null||yddUser.getPassword() == ""){
                    yddUser.setPassword(one.getPassword());
                }else {
                    yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
                }
                yddUserService.updateById(yddUser);

                return ResultData.ok("修改成功", "修改成功");
            }
        }

        return ResultData.error("修改失败");
    }
}
